package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private DeptService deptService;

    /**
     * 分页查询
     *   1.完成了基本的分页查询
     *   // TODO 1 ：需要根据emp.deptId关联查询部门名称
     *   // TODO 2 ：需要根据分页查询条件进行条件查询
     * @param param
     * @return
     */
    @Override
    public PageResult<Emp> findPage(EmpQueryParam param) {
        // 1.得到分页对象
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        // TODO 2 ： 需要根据分页查询条件进行条件查询
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<Emp>()
                .like(param.getName() != null, Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,
                        Emp::getEntryDate,
                        param.getBegin(),
                        param.getEnd()
                );
        // 2.调用分页查询方法
        this.page(page, queryWrapper);
        // TODO 1 ： 需要根据emp.deptId关联查询部门名称
        List<Emp> empList = page.getRecords();
        empList.forEach(
                emp -> {
                    // 根据员工的部门id查询部门信息
                    Integer deptId = emp.getDeptId();
                    Dept dept = deptMapper.selectById(deptId);
                    // 赋值部门名称
                    if (dept != null) {
                        emp.setDeptName(dept.getName());
                    }
                }
        );
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        emp.setDeptName(deptService.getById(emp.getDeptId()).getName());
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove(
                new LambdaQueryWrapper<EmpExpr>()
                        .eq(EmpExpr::getEmpId, emp.getId())
        );
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
    }

    @Override
    @Transactional
    public void saveEmpAndExpr(Emp emp) {
        // 1.保存主表emp信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        this.save(emp);
        // 2.保存关联表emp_expr信息
        // 2.1取出工作经历list
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList == null || exprList.size() == 0) {
            return;
        }
        // 2.2遍历list，添加员工id
        exprList.forEach(
                empExpr -> {
                    empExpr.setEmpId(emp.getId());
                }
        );
        // 2.3批量保存
        empExprService.saveBatch(exprList);
    }
}
